package com.jxw.scanpen.activity;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.KeyEvent;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.res.ResourcesCompat;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.jxw.scan_demo.R;


/**
 * 基类
 *
 * @param <DB>
 */
public abstract class BaseActivity<DB extends ViewDataBinding> extends AppCompatActivity {
    DB binding;

    private Context context;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setTheme(R.style.Theme_Souti1);
        binding = DataBindingUtil.setContentView(this, setLayoutID());

        context = this;
        init();
        getSaveData(savedInstanceState);
    }
    
    protected abstract void init();

    public abstract int setLayoutID();

    //处理 保存数据
    protected void getSaveData(Bundle savedInstanceState) {
    }

    //保存当前对象 防止意外回收
    protected void saveBean(Bundle outState, String content) {
        if (!TextUtils.isEmpty(content)) { //保存当前记录
            outState.putString("data_key", content);
        }
    }

    //界面跳转
    protected void startIntentActivity(Class className) {
        startActivity(new Intent(this, className));
    }

    public Context getContext() {
        return context;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        Log.e("lee", "keyCode:" + keyCode);
        if (keyCode == 5 || keyCode == 286 || keyCode == 27 || keyCode == 141|| keyCode == 131) {
            try {
                startIntentActivity(Class.forName("com.jxw.souti.activity.MainActivity"));
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        }
        return super.onKeyDown(keyCode, event);
    }



    public Drawable drawable(int drawable) {
        return ResourcesCompat.getDrawable(context.getResources(), drawable, null);
    }

    public int getColor(Context context, int color) {
        return ResourcesCompat.getColor(context.getResources(), color, null);
    }

    public String getString(Context context, int id) {
        return context.getString(id);
    }

    public DB getBinding() {
        return binding;
    }
}
